﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace Topro.Web.Controllers
{
    using System.Text.RegularExpressions;
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.Command;
    using Topro.Entity.Request.Inventory;
    using Topro.Util;

    /// <summary>
    /// 公共模块控制器
    /// </summary>
    public class ModuleController : BaseController
    {
        #region 注入
        /// <summary>
        /// 
        /// </summary>
        private readonly IUId _uid;

        /// <summary>
        /// 物料接口
        /// </summary>
        private readonly ProductService _productService;

        /// <summary>
        /// 托盘接口
        /// </summary>
        private readonly ContainerService _containerService;

        /// <summary>
        /// 库位接口
        /// </summary>
        private readonly WarehouseUnitService _warehouseService;


        /// <summary>
        /// 库存接口
        /// </summary>
        private readonly InventoryService _inventoryService;

        /// <summary>
        /// 指令接口
        /// </summary>
        private readonly CommandService _commandService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="productService">物料接口</param>
        /// <param name="trayService">容器接口</param>
        /// <param name="trayService">库位接口</param>
        public ModuleController(IUId uid,ProductService productService, ContainerService containerService, WarehouseUnitService warehouseService, InventoryService inventoryService, CommandService commandService)
        {
            _uid = uid;
            _productService = productService;
            _containerService = containerService;
            _warehouseService = warehouseService;
            _inventoryService = inventoryService;
            _commandService = commandService;
        }
        #endregion

        #region 物料
        public IActionResult Product()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<?></returns>
        public JsonResult ProductQuery(ProductQuery query)
        {
            //query.Status = (int)ProductStatus.启用;

            var list = _productService.ProductQuery(query);

            return Json(list);
        }
        #endregion

        #region 容器
        /// <summary>
        /// 托盘
        /// </summary>
        /// <returns></returns>
        public IActionResult Container()
        {
            return View();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<?></returns>
        public JsonResult ContainerQuery(ContainerQuery query)
        {
            //未锁定
            query.Locked = 0;

            var list = _containerService.ContainerQuery(query);

            return Json(list);
        }
        #endregion

        #region 创建入库单
        /// <summary>
        /// 创建入库订单
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateStockIn()
        {
            ViewBag.NextId = $"RK{DateTime.Now.ToString("yyyyMMddHHmmss")}";
            //状态
            ViewBag.ContainerType = (Dictionary<int, string>)EnumUtil.ToDictionary(typeof(ContainerType));

            return View("CreateStockIn");
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>DataGrid<?></returns>
        public JsonResult ProductEmptyQuery(ProductQuery query)
        {
            var list = _productService.ProductQuery(query);

            return Json(list);
        }
        #endregion

        #region 创建出库单
        /// <summary>
        /// 创建出库单
        /// </summary>
        /// <returns></returns>
        public IActionResult CreateStockOut()
        {
            //yyyyMMddHHmmss
            ViewBag.OrderId = $"CK{DateTime.Now.ToString("yyyyMMddHHmmss")}";

            return View();
        }
        #endregion

        #region 库位查询
        /// <summary>
        /// 库位查询
        /// </summary>
        /// <returns></returns>
        public IActionResult WarehouseUnit(string type)
        {
            ViewBag.Type = type;
            //状态
            ViewBag.WarehouseUnitStatus = (Dictionary<int, string>)EnumUtil.ToDictionary(typeof(WarehouseStatus));

            return View();
        }

        /// <summary>
        /// 库位查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        public IActionResult WarehouseUnitQuery(WarehouseUnitQuery query)
        {
            query.Locked = 0;

            var list = _warehouseService.Query(query);

            return Json(list);
        }
        #endregion

        #region 库存查询
        /// <summary>
        /// 库存查询
        /// </summary>
        /// <returns></returns>
        public IActionResult Inventory(InventoryQuery query)
        {
            //ViewBag.Type = query.Type.Value;

            query.OrderType = (int)InventoryType.入库;
            var list = _inventoryService.StockInQuery(query);
            return View(list);
        }

        /// <summary>
        /// 库存查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        public IActionResult InventoryQuery(InventoryQuery query)
        {
            //类型
            query.OrderType = (int)InventoryType.入库;
            //库存
            query.InventoryQty = 0;
            //状态
            query.Status = (int)InventoryStatus.完成;
            //查询
            var list = _inventoryService.InventoryQuery(query);

            return Json(list);
        }

        /// <summary>
        /// 先进先出
        /// </summary>
        /// <returns></returns>
        public JsonResult FIFO(FIFORequest request)
        {
            var response = _inventoryService.FIFO(request);

            return Json(response);
        }
        #endregion

        #region 台车
        /// <summary>
        /// 台车
        /// </summary>
        /// <returns></returns>
        public IActionResult Car()
        {
            return View();
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <returns></returns>
        public JsonResult SaveCommand(CommandSaveRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                request.Ip = Ip;
                request.UserId = UserId;

                response = _commandService.Save(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
        #endregion

        #region 物料明细
        /// <summary>
        /// 物料明细扫码
        /// </summary>
        /// <returns></returns>
        public IActionResult ProductBarCode()
        {
            return View();
        }

        /// <summary>
        /// 物料明细
        /// </summary>
        /// <returns></returns>
        public IActionResult ProductItem()
        {
            return View();
        }
        #endregion
    }
}
